<?php
	class MysqlResult {
		var $res;
		var $numRows;
		var $affectedRows;
		function MysqlResult($res,$conn) {
			$this->res = $res;
			
			if(is_resource($res))
				$this->numRows = mysql_num_rows($res);
			$this->affectedRows = mysql_affected_rows($conn);
		}
		function resultArray() {
			$arr = array();
			while($row = mysql_fetch_array($this->res,MYSQL_ASSOC)) {
				$arr[] = $this->__unescape($row);
			}
			return $arr;
		}
		function rowArray() {
			$res = mysql_fetch_array($this->res,MYSQL_ASSOC);
			return $this->__unescape($res);
		}
		function numRows() {
			return $this->numRows;
		}
		function affectedRows() {
			return $this->affectedRows;
		}
		function __unescape($arr = array()) {
			$newArr = array();
			foreach($arr as $i=>$val) {
				$newArr[$i] = stripslashes($val);
			}
			return $newArr;
		}
	}
	class Database {
		var $conn;
		var $err;
		var $last_id;
		var $lastQuery;
		function Database($host, $username, $password, $db) {
			$this->connect($host, $username, $password,$db);
		}
		function connect($host, $username, $password,$db) {
			$conn = mysql_connect($host, $username, $password);
			$dbconn = mysql_select_db($db, $conn);
			$this->conn = $conn;
		}
		function query($query, $params = array()) {
			
			$query = $this->_prepareQuery($query, $params);
			$this->lastQuery = $query;
			if($res = @mysql_query($query, $this->conn))
				return new MysqlResult($res, $this->conn);
			$this->err = "<pre>". $query . "\n" .mysql_error(). "</pre>";
			return false;
		}
		function insert($table, $data=array(), $condition=false) {
			$data = $this->_prepareUpdate($data);
			$data = implode( ',', $data);
			
			if(!$condition) {
				$sql = "INSERT INTO `$table` SET $data";
				$res = $this->query($sql);
				if(!$res)
					return false;
				$res2 = $this->query("SELECT LAST_INSERT_ID() as id");
				$row = $res2->rowArray();
				$this->last_id = $row['id']; 
				return $res;
			}
			
			$sql = "UPDATE `$table` SET $data WHERE $condition";
			return $this->query($sql);
		}
		function _prepareUpdate($data = array()) {
			$newArr = array();
			foreach($data as $i=>$value) {
				$newArr[$i] = $i."=".$this->qoute(mysql_escape_string($value));
			}
			return $newArr;
		}
		function escapeData($data = array()) {
			$newData = array();
			foreach($data as $item) {
				$newData[] = $this->qoute($item);
			}
			return $newData;
		}
		function limit($page=0, $numPerPage=0) {
			$row = ($page) * $numPerPage;
			return "LIMIT $row,$numPerPage";
		}
		function _prepareQuery($query, $params = array()) {
			if(count($params) < 1)
				return $query;
			foreach($params as $item) {
				$split = explode('?',$query,2);
				$query = $split[0] . $this->qoute(mysql_escape_string($item)). $split[1];			
			}
			return $query;
		}
		function qoute($str) {
			$parts = explode('@pre', $str);
			if(isset($parts[1]) && trim($parts[1]) == 'NOQOUTE') {
				return trim($parts[0]);	
			}
			return (is_numeric($parts[0]))?	trim($parts[0]) : "'".trim($parts[0])."'";
		}
		function last_insert_id() {
			return $this->last_id;
		}
		function lastQuery() {
			return $this->lastQuery;
		}
	}
	
?>
